package com.mg;

import com.google.protobuf.InvalidProtocolBufferException;

import java.util.ArrayList;
import java.util.List;

public class TestSubscribeReqProto {

    private static byte[] encode(SubScribeReqProto.SubScribeReq req) {
        return req.toByteArray();
    }

    private static SubScribeReqProto.SubScribeReq decode(byte[] body) throws InvalidProtocolBufferException {
        return SubScribeReqProto.SubScribeReq.parseFrom(body);
    }

    private static SubScribeReqProto.SubScribeReq createSubScribeReq() {
        SubScribeReqProto.SubScribeReq.Builder builder = SubScribeReqProto.SubScribeReq.newBuilder();
        builder.setSubReqID(1);
        builder.setUserName("fiver");
        builder.setProductName("Netty book");
        List<String> address = new ArrayList<>();
        address.add("NanJing");
        address.add("Beijing");
        address.add("Shanghai");
        builder.addAllAddress(address);
        return builder.build();
    }

    public static void main(String[] args) throws InvalidProtocolBufferException {
        SubScribeReqProto.SubScribeReq req = createSubScribeReq();
        System.out.println("Before encode：\n" + req.toString());

        SubScribeReqProto.SubScribeReq req2 = decode(encode(req));

        System.out.println("After decode：\n" + req.toString());

        System.out.println("Assert equal：--> " + req2.equals(req));
    }
}